package com.example.myapplication;

import java.io.Serializable;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class Course implements Serializable {
    private String title;
    private String credit;
    private String teacher;
    private String classroom;
    private String time;

    public Course(String title, String credit, String teacher, String classroom, String time) {
        this.title = title;
        this.credit = credit;
        this.teacher = teacher;
        this.classroom = classroom;
        this.time = time;
    }

    public String getTitle() {
        return title;
    }

    public String getCredit() {
        return credit;
    }

    public String getTeacher() {
        return teacher;
    }

    public String getClassroom() {
        return classroom;
    }

    public String getTime() {
        return time;
    }

    // 检查课程时间是否冲突
    public boolean hasTimeConflict(Course other) {
        if (this.time == null || other.time == null) {
            return false;
        }
        
        // 解析时间信息
        String[] thisTimeParts = this.time.split("-");
        String[] otherTimeParts = other.time.split("-");
        
        if (thisTimeParts.length != 2 || otherTimeParts.length != 2) {
            return false;
        }
        
        // 检查是否同一天
        if (!thisTimeParts[0].equals(otherTimeParts[0])) {
            return false;
        }
        
        // 检查节次是否重叠
        String[] thisSections = thisTimeParts[1].split(",");
        String[] otherSections = otherTimeParts[1].split(",");
        
        for (String thisSection : thisSections) {
            for (String otherSection : otherSections) {
                if (thisSection.equals(otherSection)) {
                    return true;
                }
            }
        }
        
        return false;
    }

    // 将课程对象转换为JSON对象
    public JSONObject toJSON() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("title", title);
        jsonObject.put("credit", credit);
        jsonObject.put("teacher", teacher);
        jsonObject.put("classroom", classroom);
        jsonObject.put("time", time);
        return jsonObject;
    }

    // 从JSON对象创建课程对象
    public static Course fromJSON(JSONObject jsonObject) throws JSONException {
        return new Course(
            jsonObject.getString("title"),
            jsonObject.getString("credit"),
            jsonObject.getString("teacher"),
            jsonObject.getString("classroom"),
            jsonObject.getString("time")
        );
    }

    // 将课程列表转换为JSON字符串
    public static String coursesToJson(List<Course> courses) {
        JSONArray jsonArray = new JSONArray();
        try {
            for (Course course : courses) {
                jsonArray.put(course.toJSON());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonArray.toString();
    }

    // 从JSON字符串创建课程列表
    public static List<Course> coursesFromJson(String jsonString) {
        List<Course> courses = new ArrayList<>();
        try {
            JSONArray jsonArray = new JSONArray(jsonString);
            for (int i = 0; i < jsonArray.length(); i++) {
                courses.add(fromJSON(jsonArray.getJSONObject(i)));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return courses;
    }
} 